﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using Business.WEB;
using Business.WPF;
using DataAccess;
using DataAccess.Exceptions;
using Model;
using Presenters.WPF;

namespace AirlinePilotAcedemy_WEB
{
    public partial class ViewPersonalInfoStudent : System.Web.UI.Page
    {
        DateTime birthday;




        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (!User.Identity.IsAuthenticated)
                {
                    FormsAuthentication.RedirectToLoginPage();
                }
                User user = UserDataAccess.GetUserByUsername(User.Identity.Name);
                Student student = StudentDataAccess.GetOneStudentByID(user.Ci);
                this.birthday = student.birthday;

                lastnameTB.Text = student.lastname;
                mlastnameTB.Text = student.mlastname;
                nameTB.Text = student.name;
                sexTB.Text = student.sex;
                birthdayTB.Text = student.birthday.ToShortDateString();
                birthplaceTB.Text = student.birthplace;
                idTB.Text = student.id.ToString();
                issuedInTB.Text = student.issuedIn;
                professionTB.Text = student.profession;
                liceAeroTB.Text = student.liceAero;
                addressTB.Text = student.address;
                zoneTB.Text = student.zone;
                telephoneTB.Text = student.telephone;
                emailTB.Text = student.email;
                fNameTB.Text = student.fName;
                fProfessionTB.Text = student.fProfession;
                mNameTB.Text = student.mName;
                mProfessionTB.Text = student.mProfession;
                observationsTB.Text = student.observations;

            }
        }
        protected void enableModify_Click(object sender, EventArgs e)
        {
            lastnameTB.Enabled = true;
            mlastnameTB.Enabled = true;
            nameTB.Enabled = true;
            birthplaceTB.Enabled = true;
            issuedInTB.Enabled = true;
            professionTB.Enabled = true;
            liceAeroTB.Enabled = true;
            addressTB.Enabled = true;
            zoneTB.Enabled = true;
            telephoneTB.Enabled = true;
            emailTB.Enabled = true;
            fNameTB.Enabled = true;
            fProfessionTB.Enabled = true;
            mNameTB.Enabled = true;
            mProfessionTB.Enabled = true;
            this.enableModify.Visible = false;
            this.ModifyB.Visible = true;
        }
        protected void ModifyB_Click(object sender, EventArgs e)
        {
            try
            {
                User user = UserDataAccess.GetUserByUsername(User.Identity.Name);
                Student student = StudentDataAccess.GetOneStudentByID(user.Ci);

                 StudentRegistrationManager.ModifyStudent(nameTB.Text, lastnameTB.Text, mlastnameTB.Text, sexTB.Text, birthplaceTB.Text, student.birthday,
                    issuedInTB.Text, liceAeroTB.Text, professionTB.Text, addressTB.Text, zoneTB.Text, telephoneTB.Text, emailTB.Text, Convert.ToInt32(idTB.Text),
                    fNameTB.Text, fProfessionTB.Text, mNameTB.Text, mProfessionTB.Text, observationsTB.Text);
                 Response.Write("<script>alert('Datos correctamente cambiados!!!'); window.location.href = 'StudentMenu.aspx';</script>");

            }
            catch (Exception ex)
            {
                this.messageL.Text = ex.Message;
            }

        }

        protected void telephoneTB_TextChanged(object sender, EventArgs e)
        {
            if (System.Text.RegularExpressions.Regex.IsMatch(telephoneTB.Text, "[0-9]") == false)
            {
                telephoneTB.Enabled = false;
            }
        }
    }
}